﻿using JYH.Data;
using JYH.Framework;
using System.Configuration;
using System.Globalization;
using System.Windows.Forms;
using System.Data;
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Collections.Specialized;
using System.Xml;

namespace JYH.DataAccess
{
    public class DB
    {

        private static Database _myDatabase = null;
        private static string fileName = System.Threading.Thread.GetDomain().SetupInformation.ConfigurationFile;
        private static string path = Path.GetDirectoryName(fileName);
        public static Database myDatabase
        {
            get
            {
                return _myDatabase;
            }
            set
            {
                _myDatabase = value;
            }
        }
        
        public static Database D(bool isCheck=true)
        {
          
            Database db =null;
            string Msg = string.Empty;

            if (ConfigHelper.CheckConnectionStringKey("DBCONN"))
            {
              myDatabase=  db = DB.D("DBCONN",isCheck);
            }

            
            return db;
        }
        
        public static Database D(string conconnectionStringName, bool isCheck = true)
        {
            string Msg = string.Empty;
            Database db = new Database(conconnectionStringName);
            if (isCheck==true)
            {
                if (!CheckRegist(out Msg))
                {
                    FileHelper.AppendText(Msg, path + DateTime.Now.ToString("yyyyMMddHHmmss") + @"Log.txt");
                    return null;
                }
            }
 
            return db;
        }
        public static Database D (string xmlFileName, string xmlConnStrNode, string xmlProviderNode, bool isCheck = true)
        {
            string Msg = string.Empty;
            Database db = new Database(xmlFileName,xmlConnStrNode,xmlProviderNode);
            if (isCheck)
            {
                if (!CheckRegist(out Msg))
                {
                    FileHelper.AppendText(Msg, path + DateTime.Now.ToString("yyyyMMddHHmmss") + @"Log.txt");
                    return null;
                }
            }
            return db;

        }
        public static Database D(string connectionString, string providerName,bool isCheck = true)
        {
            string Msg = string.Empty;
            Database db = new Database(connectionString, providerName);
            if (isCheck)
            {
                if (!CheckRegist(out Msg))
                {
                    FileHelper.AppendText(Msg, path + DateTime.Now.ToString("yyyyMMddHHmmss") + @"Log.txt");
                    return null;
                }
            }
            return db;
        }




        public static bool CheckRegist(out string Msg)
        {
            Msg = string.Empty;
            string AuthType=string.Empty;
            string Url = "";
            string A0="";
            IFormatProvider provider = new CultureInfo("zh-CN", true);
            try
            {

                List<string> HW = Hardware.GetCpuID();


                if (ConfigHelper.CheckAppSettingKey("AuthType"))
                {
                    AuthType = ConfigHelper.GetValue("AuthType");
                }
                if (string.Equals(AuthType.ToUpper(), "Local".ToUpper()))
                {

                    if (ConfigHelper.CheckAppSettingKey("AU"))
                    {
                        A0 = ConfigHelper.GetValue("AU");
                    }
                    else
                    { 
                    
                        if (HW.Count < 1)
                        {
                            Msg = "授权文件错误<没有取得设备信息>,联系软件厂商";
                            return false;
                        }
                        A0 = Security.DESEncrypt(HW[0].ToString() + "|" + DateTime.Now.ToString("yyyyMMdd") + "|" + DateTime.Now.AddDays(90).ToString("yyyyMMdd"));

                        string FilePath= System.Threading.Thread.GetDomain().SetupInformation.ConfigurationFile;
                        ConfigHelper.SetAppSetttingValue(FilePath,"AU", A0);
                     
                        
                    }
                    if (string.IsNullOrEmpty(Security.DESDecrypt( A0)))
                    {
                        Msg = "授权文件错误<没有授权信息>,联系软件厂商";
                        return false;
                    }
                    A0 = Security.DESDecrypt(A0);
                    string[] AS = A0.Split('|');
                    if (AS.Length < 3)
                    {
                        Msg = "授权文件错误<授权信息错误>,联系软件厂商";
                        return false;
                    }
                    DateTime dt1= DateTime.ParseExact(AS[1], "yyyyMMdd", provider);
                    if (dt1 > DateTime.Now)
                    {
                        Msg = "当前日期不能晚于授权起始日期";
                        return false;
                    }
                    DateTime dt2 = DateTime.ParseExact(AS[2], "yyyyMMdd", provider);
                    if (dt2 < DateTime.Now)
                    {
                        Msg = "软件授权已经到期,请联系软件厂商";
                        return false;
                    }
                    if (!Hardware.CheckHardWare(AS[0]))
                    {
                        Msg = "本设备没有取得授权,请联系软件厂商";
                        return false;
                    }
                    return true;
                }
                else
                {
                    if (ConfigHelper.CheckAppSettingKey("WebAPI"))
                    {
                        Url = ConfigHelper.GetValue("WebAPI") + @"/JYHWebService.asmx";
                    }
                    else
                    {
                        Url = @"http://127.0.0.1:9001/JYHWebService.ashx";
                    }
             
                    object[] arg = { ""};
                    object obj = WebServiceHelper.InvokeWebService(Url, "", "GetAuthorization", arg);
                    if (obj == null)
                    {
                        Msg = "访问授权服务器错误";
                        return false;
                    }
                    else
                    {
                        Msg=arg[0].ToString();
                       return bool.Parse(obj.ToString());
                    }

                  

                }
            }
            catch (Exception ex)
            { 
                Msg=ex.ToString();
                return false;
            }
    
           
        }
    }
}